package com.huang.qqslidemenu.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.CycleInterpolator;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.huang.qqslidemenu.Constant;
import com.huang.qqslidemenu.MyLinearLayout;
import com.huang.qqslidemenu.R;
import com.huang.qqslidemenu.SlideMenu;
import com.huang.qqslidemenu.utils.LogUtils;
import com.nineoldandroids.view.ViewHelper;
import com.nineoldandroids.view.ViewPropertyAnimator;

public class MainActivity extends AppCompatActivity {

    private ListView menuList;
    private ListView mainList;
    private SlideMenu slideMenu;
    private ImageView ivHead;
    private MyLinearLayout myLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        setContentView(R.layout.activity_main);
        menuList = (ListView) findViewById(R.id.menu_listview);
        mainList = (ListView) findViewById(R.id.main_listview);
        slideMenu = (SlideMenu) findViewById(R.id.slide_menu);
        ivHead = (ImageView) findViewById(R.id.iv_head);
        myLayout = (MyLinearLayout) findViewById(R.id.my_layout);
    }

    private void initData() {
        mainList.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, Constant.NAMES){
            @NonNull
            @Override
            public View getView(int position, View convertView, @NonNull ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                //先缩小view
                ViewHelper.setScaleX(view, 0.5f);
                ViewHelper.setScaleY(view, 0.5f);
                //以属性动画放大
                ViewPropertyAnimator.animate(view).scaleX(1).setDuration(350).start();
                ViewPropertyAnimator.animate(view).scaleY(1).setDuration(350).start();
                return view;
            }
        });
        menuList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                Constant.sCheeseStrings){
            @NonNull
            @Override
            public View getView(int position, View convertView, @NonNull ViewGroup parent) {
                /** 重新getView方法，改变字体的颜色 */
                TextView tv = (TextView) super.getView(position, convertView, parent);
                tv.setTextColor(Color.WHITE);
                return tv;
            }
        });

        slideMenu.setDragStateListener(new SlideMenu.onDragStateListener() {
            @Override
            public void open() {
                LogUtils.e("open");
            }

            @Override
            public void close() {
                LogUtils.e("close");
                /** 头像抖动动画 */
                ViewPropertyAnimator.animate(ivHead).translationXBy(15)
                        .setInterpolator(new CycleInterpolator(4))
                        .setDuration(800).start();
            }

            @Override
            public void onDragging(float percentage) {
                LogUtils.e("percentage:" + percentage);
                //头像随slideMain移动而消失
                ViewHelper.setAlpha(ivHead, 1-percentage);
            }

        });

        myLayout.setSlideMenu(slideMenu);
    }
}
